PHP Tutorial
Let's see the example to store string, integer, and float values in PHP variables.
<?php
$str="hello string";
$x=123;
$y=345.6;
echo "string is: $str
";
echo "integer is: $x
";
echo "float is: $y
";
?>
A variable declared outside a function has a Global Scope and can only be accessed outside a function
<?php
$x = 78; // global scope
function myTest() {
echo "Variable x inside function is: $x";
}
myTest();
echo "Variable x outside function is: $x";
?>
use the global keyword to access a global variable from within a function.
<?php
$x = 78; // global scope
function myTest() {
global $x;
echo "Variable x inside function is: $x";
}
myTest();
echo "Variable x outside function is: $x";
?>
A variable declared within a function has a Local Scope and can only be accessed within that function:
<?php
function myTest() {
$x = 78; // local scope
echo "Variable x inside function is: $x";
}
myTest();
echo "Variable x outside function is: $x";
?>